check_profile=false;
//Check grid was loaded - Edit profile
_grid_profile_load=false;
//Check grid was loaded - Edit meeting
_grid_meeting_load=false;
jQuery(document).ready(function(){
        _profile_detail_init();

        // uploader init
        _cases_init_dialog();

        // documents init
        _documents_init_dialog();

        // invoices
        _invoices_init_dialog();

        _meetings_init_dialog();

        // auto center the loading
        jQuery.fn.centerScreen = function(loaded) { 
            var obj = this; 
            obj.css('top', $(window).height()/2- this.height()/2); 
            obj.css('left', $(window).width()/2- this.width()/2); 
            return;
            if(!loaded) { 
                obj.css('top', $(window).height()/2- this.height()/2); 
                obj.css('left', $(window).width()/2- this.width()/2); 
                $(window).resize(function() 
                    { obj.centerScreen(!loaded); }); 
            } else { 
                obj.stop(); 
                obj.animate({ top: $(window).height()/2 - this.height()/2, left: $(window).width()/2-this.width()/2}, 200); 
            } 
        };

        // center the loading         
        //_abpv_centerLoading();
        jQuery("#abpv_loadingDiv").centerScreen(true);
        $(window).bind('resize', function() {
                jQuery("#abpv_loadingDiv").centerScreen(false);
        });
        $(window).bind('scroll', function() {
                jQuery("#abpv_loadingDiv").centerScreen(false);
        });    

        $('#client_id').change(function() {
                client_val(this);
        });
        
        //Ping - refresh session
        setInterval(function(){jQuery.post("index.php", { module: "profilemodule", action: "ping",ajax_action: 1});},180000);
});


function _abpv_showLoading(){
    jQuery('#abpv_loadingDiv').fadeIn();
}

function _abpv_hideLoading(){
    jQuery('#abpv_loadingDiv').fadeOut();
}

function client_val(client)
{
    if(!check_profile)
    return true;
    if(jQuery('#add_new').val()!=1)
    return true;
    clientid=$(client).val();
                $.ajax({
                        async:false,
                        type: "POST",
                        url: "/index.php",
                        data: { module: "profilemodule", action: "manage_edit_check_client_id",ajax_action: 1,client_id:clientid }
                }).done(function( msg ) {
                        
                        Client_lb=jQuery('#client_idControl span');
      
                        if(msg=='dup')
                            {
                            Client_lb.html(Client_lb_old+'<b id="Client_lb_" style="color:red"> Duplicate!</b>');                            
                            res= false;
                        }
                        else
                        if(msg=='no')
                            {
                            Client_lb.html(Client_lb_old+'<b id="Client_lb_" style="color:red"> Error!</b>');                            
                            res= false;
                        }
                        else
                            {
                            Client_lb.html(Client_lb_old+'<b id="Client_lb_" style="color:green"> Ok!</b>');                           
                            res= true;
                        }
                });
                return res;
}

/* Build the dialog */
function _profile_detail_init()
{
    $('#profile_dialog_detail').dialog({
            title:'User Detail',
            width: 1000,
            height: 700,
            closed: true,  
            modal: true,
            cache: false,
            buttons:[{
                    text:'Ok',
                    iconCls:'icon-ok',
                    handler:function(){
                        _grid_profile_load=false;
                        jQuery("#profile_dialog_detail").dialog("close");
                    }
            }],
            onClose:function()
            {
                _grid_profile_load=false;
            }
    });

    //init cases data grid
    jQuery('#_tab_profile_cases').datagrid({  
            title:'List of client cases',
            autoRowHeight: true, 
            idField: "id",
            nowrap: false,
            height: 565,
            pageNumber: 1,
            pageSize: 20,
            pagination: 1,
            rownumbers: true,
            singleSelect: true, 
            columns:[[  
                    {field:'name',title:'Case name',width:140, sortable:true},  
                    {field:'poster',title:'Created by',width:150, sortable:true},  
                    {field:'post_date',title:'Post date',width:140,align:'right',sortable:true, formatter: function(value,row,index){
                            return row.post_date_text;
                    }},
                    {field:'last_update',title:'Last update',width:150,align:'right',sortable:true, formatter: function(value,row,index){
                            return row.last_update_text;
                    }} , 
                    {field:'last_update_by',title:'Last update by',width:100,align:'right', sortable:true}  ,
                    {field:'pdf_file',title:'PDF & Video',width:100,align:'right', formatter: function(value,row,index){
                            var html="";
                            if (row.file_video!="")
                                html+="<a href='"+row.file_video+"' class='icon-video grid_a' target=_blank>&nbsp;</a>";
                            if (row.file_pdf!= "")
                                html+="<a href='"+row.file_pdf+"' class='icon-pdf grid_a' target=_blank>&nbsp;</a>"
                            return html;
                    }},
                ]]  ,
            onBeforeLoad: function(param){
                if(!_grid_profile_load)
                return false;
                // get selected user in main grid
                var node = __FormHelper_GetGridObject();
                if (!(node.length > 0))
                    return;
                node = node.datagrid('getSelected');

                var url="/index.php?module=profilemodule&action=manage_cases_ajax&ajax_action=1&user_id="+node.user_id+"&src="+__FormHelper_GetSrc();
                // build URL to reload
                var options=jQuery(this).datagrid("options");

                var search_name=jQuery("#_case_form_advancesearch_name").val();

                if(search_name!=''){
                    url += "&search="+search_name;
                }
                if (options.sortName != null){
                    url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
                }
                // date search type
                var date_type =jQuery("#_case_form_advancesearch_datetype").combobox("getValue");
                var date_from=jQuery("#_case_form_advancesearch_datefrom").datebox("getValue");
                var date_to=jQuery("#_case_form_advancesearch_dateto").datebox("getValue");
                url+="&datefrom="+date_from+"&dateto="+date_to+"&datetype="+date_type;

                var file_type =jQuery("#_case_form_advancesearch_filetype").combobox("getValue");
                url+="&filetype="+file_type;

                url+="&_"+Math.random();

                jQuery(this).datagrid('options').url = url;
            }
    }); 

    // init documents
    jQuery('#_tab_profile_documents').datagrid({  
            title:'List of client documents',
            autoRowHeight: true, 
            idField: "id",
            nowrap: false,
            height: 565,
            pageNumber: 1,
            pageSize: 20,
            pagination: 1,
            rownumbers: true,
            singleSelect: true, 
            columns:[[  
                    {field:'name',title:'Document name',width:200, sortable:true},
                    {field:'create_by',title:'Create by',width:160, sortable:true},
                    {field:'post_date',title:'Create on',width:160, sortable:true, formatter: function(value,row,index){
                            return row.post_date_text;
                    }},
                    {field:'file_list',title:'Media files',width:250, formatter: function(value,row,index){
                            var html="";
                            if (row.flv_file!="")
                                html+="<a href='"+row.flv_file+"' class='icon-video grid_a' target=_blank>&nbsp;</a>";
                            if (row.mp3_file!= "")
                                html+="<a href='"+row.mp3_file+"' class='icon-mp3 grid_a' target=_blank>&nbsp;</a>"
                            if (row.pdf_file!= "")
                                html+="<a href='"+row.pdf_file+"' class='icon-pdf grid_a' target=_blank>&nbsp;</a>"
                            if (row.doc_file!= "")
                                html+="<a href='"+row.doc_file+"' class='icon-doc grid_a' target=_blank>&nbsp;</a>"
                            if (row.xls_file!= "")
                                html+="<a href='"+row.xls_file+"' class='icon-xls grid_a' target=_blank>&nbsp;</a>"
                            if (row.ppt_file!= "")
                                html+="<a href='"+row.ppt_file+"' class='icon-ppt grid_a' target=_blank>&nbsp;</a>"
                            if (row.img_file!= "")
                                html+="<a href='"+row.img_file+"' class='icon-img grid_a' target=_blank>&nbsp;</a>"
                            return html;
                    }},
                ]],
            onBeforeLoad: function(param){
                if(!_grid_profile_load)
                return false;
                // get selected user in main grid
                var node = __FormHelper_GetGridObject();
                if (!(node.length > 0))
                    return;
                node = node.datagrid('getSelected');

                var url="/index.php?module=profilemodule&action=manage_documents_ajax&ajax_action=1&user_id="+node.user_id+"&src="+__FormHelper_GetSrc();
                // build URL to reload
                var options=jQuery(this).datagrid("options");

                var search_name=jQuery("#_document_form_advancesearch_name").val();

                if(search_name!=''){
                    url += "&search="+search_name;
                }
                if (options.sortName != null){
                    url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
                }
                // date search type
                var date_from=jQuery("#_document_form_advancesearch_datefrom").datebox("getValue");
                var date_to=jQuery("#_document_form_advancesearch_dateto").datebox("getValue");
                url+="&datefrom="+date_from+"&dateto="+date_to;

                var file_type =jQuery("#_case_form_advancesearch_filetype").combobox("getValue");
                url+="&filetype="+file_type;

                url+="&_"+Math.random();

                jQuery(this).datagrid('options').url = url;
            }  
    });  

    // init invoices
    jQuery('#_tab_profile_invoices').datagrid({  
            title:'List of client invoices',
            autoRowHeight: true, 
            idField: "id",
            nowrap: false,
            height: 565,
            pageNumber: 1,
            pageSize: 20,
            pagination: 1,
            rownumbers: true,
            singleSelect: true, 
            columns:[[  
                    {field:'name',title:'Invoice name',width:200, sortable:true},
                    {field:'create_by',title:'Create by',width:160, sortable:true},
                    {field:'post_date',title:'Create on',width:160, sortable:true, formatter: function(value,row,index){
                            return row.post_date_text;
                    }},
                    {field:'file_list',title:'Invoice file',width:250, formatter: function(value,row,index){
                            var html="";
                            if (row.pdf_file!= "")
                                html+="<a href='"+row.pdf_file+"' class='icon-pdf grid_a' target=_blank>&nbsp;</a>";
                            if (row.img_file!= "")
                                html+="<a href='"+row.img_file+"' class='icon-img grid_a' target=_blank>&nbsp;</a>"
                            return html;
                    }},
                ]],
            onBeforeLoad: function(param){
                if(!_grid_profile_load)
                return false;
                // get selected user in main grid
                var node = __FormHelper_GetGridObject();
                if (!(node.length > 0))
                    return;
                node = node.datagrid('getSelected');

                var url="/index.php?module=profilemodule&action=manage_invoices_ajax&ajax_action=1&user_id="+node.user_id+"&src="+__FormHelper_GetSrc();
                // build URL to reload
                var options=jQuery(this).datagrid("options");

                var search_name=jQuery("#_invoice_form_advancesearch_name").val();

                if(search_name!=''){
                    url += "&search="+search_name;
                }
                if (options.sortName != null){
                    url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
                }
                // date search type
                var date_from=jQuery("#_document_form_advancesearch_datefrom").datebox("getValue");
                var date_to=jQuery("#_document_form_advancesearch_dateto").datebox("getValue");
                url+="&datefrom="+date_from+"&dateto="+date_to;

                var file_type =jQuery("#_invoice_form_advancesearch_filetype").combobox("getValue");
                url+="&filetype="+file_type;

                url+="&_"+Math.random();

                jQuery(this).datagrid('options').url = url;
            }    
    });

    // metting
    jQuery('#_tab_profile_meeting').datagrid({  
            title:'List of meeting request by this client',
            idField: "id",
            height: 565,
            pageNumber: 1,
            pageSize: 20,
            pagination: 1,
            rownumbers: true,
            singleSelect: true, 
            autoRowHeight: true, 
            nowrap: true,
            columns:[[  
                    {field:'approve_date',title:'Approve on',width:140, sortable:true , hidden: true, formatter: function(value,row,index){
                            return row.approve_date_text;
                    }},
                    {field:'approve_by',title:'Approve by',width:100, hidden: true},
                    {field:'approve_state',title:'Approve state',width:100,hidden: true, sortable:true, formatter: function(value,row,index){
                            if (value == 0)
                                return "New request";
                            else if (value == 1)
                                return "Not approved";
                            else if (value == 2)
                                return "Approved";
                            else return "Meeting done";
                    }},
                    {field:'approve_note',title:'Approve note',width:150, hidden: true},
                    {field:'request_date',title:'Request on',width:140, sortable:true, formatter: function(value,row,index){
                            return row.request_date_text;
                    }},
                    {field:'user_target',title:'Request by',width:100},
                    {field:'meeting_date',title:'Meeting on',width:140, sortable:true, formatter: function(value,row,index){
                            return row.meeting_date_text;
                    }},
                    {field:'meeting_duration',title:'Duration',width:55, sortable:true, formatter: function(value,row,index){
                            return row.meeting_duration+"m";
                    }},
                    {field:'meeting_place',title:'Place',width:100},
                    {field:'meeting_language',title:'Language',width:80},
                    {field:'meeting_type',title:'Meeting type',width:80},
                    

                    {field:'meeting_record',title:'Record',width:50, hidden: true, formatter: function(value,row,index){
                            if (value==0)
                                return "No";
                            else
                                return "Yes";
                    }},                 
                    
                    {field:'related_lawyers',title:'Related Lawyers',width:80, hidden: true, formatter: function(value,row,index){
                            if (value==null)
                                return "No";
                            else
                                return "Yes";
                    }},

                    
                    {field:'meeting_notes',title:'Notes',width:100, hidden: true},
                    {field:'last_update',title:'Last update',width:140, sortable:true, hidden: true, formatter: function(value,row,index){
                            return row.last_update_text;
                    }},
                    {field:'last_update_by',title:'Last update by', sortable:true,width:100, hidden: true},

                    {field:'mentor_request',title:'Mentor Request',width:50, hidden: true, formatter: function(value,row,index){
                            if (value==0)
                                return "No";
                            else
                                return "Yes";
                    }},
                    
                    {field:'translator_request',title:'Translator request',width:50, hidden: true, formatter: function(value,row,index){
                            if (value==0)
                                return "No";
                            else
                                return "Yes";
                    }},                    
                    
                    {field:'mentor_required',title:'Mentor required',width:50, hidden: true},
                    {field:'fake_column',title:'&nbsp;',width:50},
                ]],
            onBeforeLoad: function(param){
                if(!_grid_profile_load)
                return false;
                // get selected user in main grid
                var node = __FormHelper_GetGridObject();
                if (!(node.length > 0))
                    return;
                node = node.datagrid('getSelected');

                var url="/index.php?module=profilemodule&action=manage_meeting_ajax&ajax_action=1&user_id="+node.user_id+"&src="+__FormHelper_GetSrc();
                // build URL to reload
                var options=jQuery(this).datagrid("options");

                // get search value
                var search_value = jQuery("#_meeting_advancesearch_name").searchbox("getValue");
                var search_type = jQuery("#_meeting_advancesearch_name").searchbox("getName");

                if(search_value !='' && search_value != 'Please enter your search'){
                    url += "&search="+search_value+"&search_type="+search_type;
                }
                if (options.sortName != null){
                    url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
                }

                // date search type
                var date_from =jQuery("#_meeeting_form_advancesearch_datefrom").datebox("getValue");
                var date_to=jQuery("#_meeting_form_advancesearch_dateto").datebox("getValue");


                url+="&datefrom="+date_from+"&dateto="+date_to;

                var status =jQuery("#_meeting_form_advancesearch_status").combobox("getValue");
                url+="&status="+status;

                url+="&_"+Math.random();

                jQuery(this).datagrid('options').url = url;
            },
            onDblClickCell: function(index,field,value){
                _meeting_edit();
            }
    });
}

function _profile_edit(add_new)
{
    var src=__FormHelper_GetSrc();
    var url="/index.php?module=profilemodule&action=manage_edit&ajax_action=1&src="+src;

    var node = __FormHelper_GetGridObject().datagrid('getSelected');
    if (node == null || add_new==true)
        {
            check_profile=true;
        SetWindowPanelTitle("profile_dialog_detail", "Add new client");
        // hide some tab
        $('#_profile_dialog_detail_maintab').tabs('disableTab', 1);
        $('#_profile_dialog_detail_maintab').tabs('disableTab', 2);
        $('#_profile_dialog_detail_maintab').tabs('disableTab', 3);
        $('#_profile_dialog_detail_maintab').tabs('disableTab', 4);
    }
    else
        {
        _grid_profile_load=true;
        var  name = node.firstname+" "+node.lastname+" ("+node.email+")";
        url+="&id="+node.user_id;  

        SetWindowPanelTitle("profile_dialog_detail", "View "+name+" profile");

        SetGridPanelTitle("_tab_profile_cases_wrapper", "List of "+name+" cases");
        _cases_clearfilter();
        _cases_reload();

        SetGridPanelTitle("_tab_profile_invoices_wrapper", "List of "+name+" invoices");
        _invoices_clearfilter();
        _invoices_reload();

        SetGridPanelTitle("_tab_profile_documents_wrapper", "List of "+name+" documents");
        _documents_clearfilter();
        _documents_reload();

        SetGridPanelTitle("_tab_profile_meeting_wrapper", "List of "+name+" meeting request");
        _meeting_clearfilter();
        _meeting_reload();

        // unhide sometab
        $('#_profile_dialog_detail_maintab').tabs('enableTab', 1);
        $('#_profile_dialog_detail_maintab').tabs('enableTab', 2);
        $('#_profile_dialog_detail_maintab').tabs('enableTab', 3);
        $('#_profile_dialog_detail_maintab').tabs('enableTab', 4);
    }

    $('#_profile_dialog_detail_maintab').tabs('select', 0);

    // for tab profile detail
    _abpv_showLoading();
    jQuery.get(url, function(data) {

            jQuery("#_tab_profile_detail").html(data);
            _abpv_hideLoading();
            // handle form submit for profile edit
            jQuery("#profile_form").find("input[name='firstname'], input[name='lastname'], input[name='email'], input[name='tel1']").addClass("required");
            
            jQuery("#profile_form").validate();

            jQuery("#profile_form").submit(function(){
                    if (!client_val('#client_id')||!jQuery(this).valid())
                    {
                    }
                    else
                        {
                        _abpv_showLoading();
                        // post back to server
                        jQuery.post('/index.php',jQuery("#profile_form").serialize(),
                            function(data) {
                                _abpv_hideLoading();
                                data = ajax_string_dim(data);
                                if (data == "OK"){
                                    alert("Profile saved.");
                                    __FormHelper_GetGridObject().datagrid("reload");
                                    jQuery("#profile_dialog_detail").dialog("close");
                                }
                                else{
                                    alert("Profile save failed.");
                                }
                        });
                    }
                    return false;
            });
    });
    jQuery('#profile_dialog_detail').dialog("open");

    /*jQuery('#profile_dialog_edit').dialog("open");
    jQuery('#profile_dialog_edit').dialog("refresh", url);*/
}

/* End Build the dialog */

/* common functions */
var _cases_pdf_uploader;
var _case_video_uploader;
function _cases_init_dialog(){

    // init uploader for cases
    _cases_pdf_uploader = new qq.FileUploader({
            element: document.getElementById('_case_pdf_uploader'),
            multiple: false, 
            uploadButtonText:"Select PDF file",
            dragText:"Drag PDF file into this area",
            allowedExtensions: ["pdf"],
            onSubmit: function(id, name){
                var node = __FormHelper_GetGridObject();
                node = node.datagrid('getSelected');
                _cases_pdf_uploader.setParams({ 
                        user_id: node.user_id,
                        ajax_action: 1,
                        module: "profilemodule",
                        action: "manage_handleupload",
                        src: __FormHelper_GetSrc()
                });
            },
            onComplete: function (id, fileName, responseJSON){
                if (responseJSON.file_path != undefined)
                    {
                    jQuery("#_cases_form_filepdf").val(responseJSON.file_path);
                }
            },
            action: '/index.php'
    });

    // video upload
    _case_video_uploader = new qq.FileUploader({
            element: document.getElementById('_case_video_uploader'),
            multiple: false, 
            uploadButtonText:"Select video file",
            dragText:"Drag your video file into this area",
            allowedExtensions: ["mp4", "flv", "avi", "mpg"],
            onSubmit: function(id, name){
                var node = __FormHelper_GetGridObject();
                node = node.datagrid('getSelected');
                _case_video_uploader.setParams({ 
                        user_id: node.user_id,
                        ajax_action: 1,
                        module: "profilemodule",
                        action: "manage_handleupload",
                        src: __FormHelper_GetSrc()
                });
            },
            onComplete: function (id, fileName, responseJSON){
                if (responseJSON.file_path != undefined)
                    {
                    jQuery("#_cases_form_filevideo").val(responseJSON.file_path);
                }
            },
            action: '/index.php'
    });

    // cases edit form save handler
    jQuery("#cases_edit_form").submit(function(){
            // check text field valid or not

            // get selected user
            var node = __FormHelper_GetGridObject();
            node = node.datagrid('getSelected');

            // then post back to server to save
            jQuery.post('/index.php',{
                    module: "profilemodule",
                    action: "manage_cases_save",
                    ajax_action: 1,
                    src: __FormHelper_GetSrc(),
                    user_id: node.user_id,
                    cases_name: jQuery("#_cases_form_name").val(),
                    pdffile: jQuery("#_cases_form_filepdf").val(),
                    videofile: jQuery("#_cases_form_filevideo").val(),
                    cases_id : jQuery("#_case_form_caseid").val(),
                    t:Math.random()
                },
                function(data) {
                    data = ajax_string_dim(data);
                    if (data != "OK")
                        {
                        alert("Case save error.");
                    }
                    else
                        {
                        // reload cases grid
                        jQuery('#_tab_profile_cases').datagrid("reload");
                        // close this dialog
                        jQuery('#_cases_dialog_edit').dialog("close");

                    }
            });

            return false; 
    });
}   

function _cases_clearfilter()
{
    try{
        jQuery("#_case_form_advancesearch_name").val("");
        jQuery("#_case_form_advancesearch_datefrom").datebox("setValue","");
        jQuery("#_case_form_advancesearch_dateto").datebox("setValue","");
        jQuery("#_case_form_advancesearch_filetype").combobox("setValue","none");
    }
    catch (err)
    {

    }
}

function _cases_remove()
{
    var node=jQuery('#_tab_profile_cases').datagrid("getSelected");
    if (node != null)
        {
        if (!confirm("Do you want to delete case '"+node.name+"'?"))
            return;

        var id=node.id;
        jQuery.post('/index.php',{
                module: "profilemodule",
                action: "manage_cases_delete",
                ajax_action: 1,
                src: __FormHelper_GetSrc(),
                id: id,
                t:Math.random()
            },function (data){
                _cases_reload();
        });
    }
}

function _cases_reload()
{
    jQuery('#_tab_profile_cases').datagrid("reload");
}

function _cases_edit(add_new){
    // need to clear old data first
    _cases_pdf_uploader.clearStoredFiles();
    _case_video_uploader.clearStoredFiles();
    jQuery("#_cases_form_name").val("");
    jQuery("#_cases_form_filepdf").val("");
    jQuery("#_cases_form_filevideo").val("");
    jQuery("#_case_form_caseid").val("0");

    // load edit data if available
    var node=jQuery('#_tab_profile_cases').datagrid("getSelected");
    SetWindowPanelTitle("_cases_dialog_edit","Add case");
    if (add_new != true && node != null)
        {
        SetWindowPanelTitle("_cases_dialog_edit","Edit case "+node.name);
        jQuery("#_cases_form_name").val(node.name);
        jQuery("#_cases_form_filepdf").val(node.file_pdf);
        jQuery("#_cases_form_filevideo").val(node.file_video);
        jQuery("#_case_form_caseid").val(node.id);
    }
    else if (add_new != true && node == null)
        // add_new == false ; node == null
        {
        return;
    }
    // then show 
    jQuery('#_cases_dialog_edit').dialog("open");
}


/** Documents process */
var _documents_uploader_pdf;
var _documents_uploader_doc;
var _documents_uploader_ppt;
var _documents_uploader_xls;
var _documents_uploader_mp3;
var _documents_uploader_doc;
function _documents_init_dialog(){
    // init uploader for cases
    // init pdf uploader
    _documents_uploader_pdf = new qq.FileUploader({
            element: document.getElementById('_documents_uploader_pdf'),
            multiple: false, 
            uploadButtonText:"Select PDF file",
            dragText:"Drag PDF file into this area",
            allowedExtensions: ["pdf"],
            onSubmit: function(id, name){
                var node = __FormHelper_GetGridObject();
                node = node.datagrid('getSelected');
                _documents_uploader_pdf.setParams({ 
                        user_id: node.user_id,
                        ajax_action: 1,
                        module: "profilemodule",
                        action: "manage_handleupload",
                        src: __FormHelper_GetSrc()
                });
            },
            onComplete: function (id, fileName, responseJSON){
                if (responseJSON.file_path != undefined)
                    {
                    jQuery("#_documents_form_pdf").val(responseJSON.file_path);
                }
            },
            action: '/index.php'
    });

    // doc uploader
    _documents_uploader_doc = new qq.FileUploader({
            element: document.getElementById('_documents_uploader_doc'),
            multiple: false, 
            uploadButtonText:"Select DOC, DOCX file",
            dragText:"Drag DOC, DOCX file into this area",
            allowedExtensions: ["doc", "docx"],
            onSubmit: function(id, name){
                var node = __FormHelper_GetGridObject();
                node = node.datagrid('getSelected');
                _documents_uploader_doc.setParams({ 
                        user_id: node.user_id,
                        ajax_action: 1,
                        module: "profilemodule",
                        action: "manage_handleupload",
                        src: __FormHelper_GetSrc()
                });
            },
            onComplete: function (id, fileName, responseJSON){
                if (responseJSON.file_path != undefined)
                    {
                    jQuery("#_documents_form_doc").val(responseJSON.file_path);
                }
            },
            action: '/index.php'
    });

    // ppt uploader
    _documents_uploader_ppt = new qq.FileUploader({
            element: document.getElementById('_documents_uploader_ppt'),
            multiple: false, 
            uploadButtonText:"Select PPT, PPTX file",
            dragText:"Drag PPT, PPTX file into this area",
            allowedExtensions: ["ppt", "pptx"],
            onSubmit: function(id, name){
                var node = __FormHelper_GetGridObject();
                node = node.datagrid('getSelected');
                _documents_uploader_ppt.setParams({ 
                        user_id: node.user_id,
                        ajax_action: 1,
                        module: "profilemodule",
                        action: "manage_handleupload",
                        src: __FormHelper_GetSrc()
                });
            },
            onComplete: function (id, fileName, responseJSON){
                if (responseJSON.file_path != undefined)
                    {
                    jQuery("#_documents_form_ppt").val(responseJSON.file_path);
                }
            },
            action: '/index.php'
    });

    // xls uploader
    _documents_uploader_xls = new qq.FileUploader({
            element: document.getElementById('_documents_uploader_xls'),
            multiple: false, 
            uploadButtonText:"Select XLS, XLSX file",
            dragText:"Drag XLS, XLSX file into this area",
            allowedExtensions: ["xls", "XLSx"],
            onSubmit: function(id, name){
                var node = __FormHelper_GetGridObject();
                node = node.datagrid('getSelected');
                _documents_uploader_xls.setParams({ 
                        user_id: node.user_id,
                        ajax_action: 1,
                        module: "profilemodule",
                        action: "manage_handleupload",
                        src: __FormHelper_GetSrc()
                });
            },
            onComplete: function (id, fileName, responseJSON){
                if (responseJSON.file_path != undefined)
                    {
                    jQuery("#_documents_form_xls").val(responseJSON.file_path);
                }
            },
            action: '/index.php'
    });

    // mp3 uploader
    _documents_uploader_mp3 = new qq.FileUploader({
            element: document.getElementById('_documents_uploader_mp3'),
            multiple: false, 
            uploadButtonText:"Select mp3 file",
            dragText:"Drag mp3 file into this area",
            allowedExtensions: ["mp3"],
            onSubmit: function(id, name){
                var node = __FormHelper_GetGridObject();
                node = node.datagrid('getSelected');
                _documents_uploader_mp3.setParams({ 
                        user_id: node.user_id,
                        ajax_action: 1,
                        module: "profilemodule",
                        action: "manage_handleupload",
                        src: __FormHelper_GetSrc()
                });
            },
            onComplete: function (id, fileName, responseJSON){
                if (responseJSON.file_path != undefined)
                    {
                    jQuery("#_documents_form_mp3").val(responseJSON.file_path);
                }
            },
            action: '/index.php'
    });

    // flv uploader
    _documents_uploader_flv = new qq.FileUploader({
            element: document.getElementById('_documents_uploader_flv'),
            multiple: false, 
            uploadButtonText:"Select flv, mpg, avi, wmv file",
            dragText:"Drag flv, mpg, avi, wmv files into this area",
            allowedExtensions: ["flv", "mpg", "avi", "vob", "wmv"],
            onSubmit: function(id, name){
                var node = __FormHelper_GetGridObject();
                node = node.datagrid('getSelected');
                _documents_uploader_flv.setParams({ 
                        user_id: node.user_id,
                        ajax_action: 1,
                        module: "profilemodule",
                        action: "manage_handleupload",
                        src: __FormHelper_GetSrc()
                });
            },
            onComplete: function (id, fileName, responseJSON){
                if (responseJSON.file_path != undefined)
                    {
                    jQuery("#_documents_form_flv").val(responseJSON.file_path);
                }
            },
            action: '/index.php'
    });
    
    // img, other files
    _documents_uploader_img = new qq.FileUploader({
            element: document.getElementById('_documents_uploader_img'),
            multiple: false, 
            uploadButtonText:"Select any filetype to upload",
            dragText:"Drag images or any filetype to upload",
            onSubmit: function(id, name){
                var node = __FormHelper_GetGridObject();
                node = node.datagrid('getSelected');
                _documents_uploader_img.setParams({ 
                        user_id: node.user_id,
                        ajax_action: 1,
                        module: "profilemodule",
                        action: "manage_handleupload",
                        src: __FormHelper_GetSrc()
                });
            },
            onComplete: function (id, fileName, responseJSON){
                if (responseJSON.file_path != undefined)
                    {
                    jQuery("#_documents_form_img").val(responseJSON.file_path);
                }
            },
            action: '/index.php'
    });

    // documents edit form save handler
    jQuery("#documents_edit_form").submit(function(){
            // check text field valid or not

            // get selected user
            var node = __FormHelper_GetGridObject();
            node = node.datagrid('getSelected');

            // then post back to server to save
            jQuery.post('/index.php',{
                    module: "profilemodule",
                    action: "manage_documents_save",
                    ajax_action: 1,
                    src: __FormHelper_GetSrc(),
                    user_id: node.user_id,
                    documents_id: jQuery("#_documents_form_id").val(),
                    t:Math.random(),
                    documents_name: jQuery("#_documents_form_name").val(),
                    xls_file: jQuery("#_documents_form_xls").val(),
                    doc_file: jQuery("#_documents_form_doc").val(),
                    ppt_file: jQuery("#_documents_form_ppt").val(),
                    pdf_file: jQuery("#_documents_form_pdf").val(),
                    flv_file: jQuery("#_documents_form_flv").val(),
                    mp3_file: jQuery("#_documents_form_mp3").val(),
                    img_file: jQuery("#_documents_form_img").val()
                },
                function(data) {
                    data = ajax_string_dim(data);
                    if (data != "OK")
                        {
                        alert("Document save error.");
                    }
                    else
                        {
                        // reload cases grid
                        jQuery('#_tab_profile_documents').datagrid("reload");
                        // close this dialog
                        jQuery('#_documents_dialog_edit').dialog("close");
                    }
            });

            return false; 
    });
}   

function _documents_clearfilter()
{
    try{
        jQuery("#_document_form_advancesearch_name").val("");
        jQuery("#_document_form_advancesearch_datefrom").datebox("setValue","");
        jQuery("#_document_form_advancesearch_dateto").datebox("setValue","");
        jQuery("#_document_form_advancesearch_filetype").combobox("setValue","none");
    }
    catch (err)
    {

    }
}

function _documents_remove()
{
    var node=jQuery('#_tab_profile_documents').datagrid("getSelected");
    if (node != null)
        {
        if (!confirm("Do you want to delete document '"+node.name+"'?"))
            return;

        var id=node.id;
        jQuery.post('/index.php',{
                module: "profilemodule",
                action: "manage_documents_delete",
                ajax_action: 1,
                src: __FormHelper_GetSrc(),
                id: id,
                t:Math.random()
            },function (data){
                _documents_reload();
        });
    }
}

function _documents_reload()
{
    jQuery('#_tab_profile_documents').datagrid("reload");
}

function _documents_edit(add_new){
    // need to clear old data first
    jQuery("#_documents_form_name").val("");
    jQuery("#_documents_form_xls").val("");
    _documents_uploader_xls.clearStoredFiles();

    jQuery("#_documents_form_doc").val("");
    _documents_uploader_doc.clearStoredFiles();

    jQuery("#_documents_form_ppt").val("");
    _documents_uploader_ppt.clearStoredFiles();

    jQuery("#_documents_form_pdf").val("");
    _documents_uploader_pdf.clearStoredFiles();

    jQuery("#_documents_form_flv").val("");
    _documents_uploader_flv.clearStoredFiles();
    
    jQuery("#_documents_form_img").val("");
    _documents_uploader_img.clearStoredFiles();

    jQuery("#_documents_form_mp3").val("");
    _documents_uploader_mp3.clearStoredFiles();
    jQuery("#_documents_form_id").val(0);
    // load edit data if available
    var node=jQuery('#_tab_profile_documents').datagrid("getSelected");
    SetWindowPanelTitle("_documents_dialog_edit","Add document");
    if (node != null && add_new!=true)
        {
        SetWindowPanelTitle("_documents_dialog_edit","Edit document "+node.name);
        jQuery("#_documents_form_name").val(node.name);
        jQuery("#_documents_form_xls").val(node.xls_file);
        jQuery("#_documents_form_doc").val(node.doc_file);
        jQuery("#_documents_form_ppt").val(node.ppt_file);
        jQuery("#_documents_form_pdf").val(node.pdf_file);
        jQuery("#_documents_form_flv").val(node.flv_file);
        jQuery("#_documents_form_img").val(node.img_file);
        jQuery("#_documents_form_mp3").val(node.mp3_file);
        jQuery("#_documents_form_id").val(node.id);
    }
    else if (add_new != true && node == null)
        // add_new == false ; node == null
        {
        return;
    }
    // then show 
    jQuery('#_documents_dialog_edit').dialog("open");
}
/** End documents process*/

/* invoices functions */
var _invoices_pdf_uploader;
var _invoices_img_uploader;
function _invoices_init_dialog(){

    // init uploader for invoices
    _invoices_pdf_uploader = new qq.FileUploader({
            element: document.getElementById('_invoice_pdf_uploader'),
            multiple: false, 
            uploadButtonText:"Select PDF file",
            dragText:"Drag PDF file into this area",
            allowedExtensions: ["pdf"],
            onSubmit: function(id, name){
                var node = __FormHelper_GetGridObject();
                node = node.datagrid('getSelected');
                _invoices_pdf_uploader.setParams({ 
                        user_id: node.user_id,
                        ajax_action: 1,
                        module: "profilemodule",
                        action: "manage_handleupload",
                        src: __FormHelper_GetSrc()
                });
            },
            onComplete: function (id, fileName, responseJSON){
                if (responseJSON.file_path != undefined)
                    {
                    jQuery("#_invoices_form_filepdf").val(responseJSON.file_path);
                }
            },
            action: '/index.php'
    });
    _invoices_img_uploader = new qq.FileUploader({
            element: document.getElementById('_invoices_img_uploader'),
            multiple: false, 
            uploadButtonText:"Select image file or zip file",
            dragText:"Drag image or zip file into this area",
            allowedExtensions: ["png","jpg","gif", "jpeg", "bmp", "ttf", "zip", "rar"],
            onSubmit: function(id, name){
                var node = __FormHelper_GetGridObject();
                node = node.datagrid('getSelected');
                _invoices_img_uploader.setParams({ 
                        user_id: node.user_id,
                        ajax_action: 1,
                        module: "profilemodule",
                        action: "manage_handleupload",
                        src: __FormHelper_GetSrc()
                });
            },
            onComplete: function (id, fileName, responseJSON){
                if (responseJSON.file_path != undefined)
                    {
                    jQuery("#_invoices_form_fileimg").val(responseJSON.file_path);
                }
            },
            action: '/index.php'
    });
    // invoices edit form save handler
    jQuery("#invoices_edit_form").submit(function(){
            // check text field valid or not

            // get selected user
            var node = __FormHelper_GetGridObject();
            node = node.datagrid('getSelected');
            // get invoices id
            var invoice_id=0;
            var invoice_selected = jQuery('#_tab_profile_invoices').datagrid("getSelected");
            if (invoice_selected != null)
                invoice_id = invoice_selected.id;

            // then post back to server to save
            jQuery.post('/index.php',{
                    module: "profilemodule",
                    action: "manage_invoices_save",
                    ajax_action: 1,
                    src: __FormHelper_GetSrc(),
                    user_id: node.user_id,
                    invoices_name: jQuery("#_invoices_form_name").val(),
                    pdf_file: jQuery("#_invoices_form_filepdf").val(),
                    img_file: jQuery("#_invoices_form_fileimg").val(),
                    invoices_id : jQuery("#_invoices_form_invoice_id").val(),
                    t:Math.random()
                },
                function(data) {
                    data = ajax_string_dim(data);
                    if (data != "OK")
                        {
                        alert("Invoice save error.");
                    }
                    else
                        {
                        // reload invoices grid
                        jQuery('#_tab_profile_invoices').datagrid("reload");
                        // close this dialog
                        jQuery('#_invoices_dialog_edit').dialog("close");
                    }
            });

            return false; 
    });
}   

function _invoices_clearfilter()
{
    try{
        jQuery("#_invoice_form_advancesearch_name").val("");
        jQuery("#_invoice_form_advancesearch_datefrom").datebox("setValue","");
        jQuery("#_invoice_form_advancesearch_dateto").datebox("setValue","");
        jQuery("#_invoice_form_advancesearch_filetype").combobox("setValue","none");
    }
    catch (err)
    {

    }
}

function _invoices_remove()
{
    var node=jQuery('#_tab_profile_invoices').datagrid("getSelected");
    if (node != null)
        {
        if (!confirm("Do you want to delete invoice '"+node.name+"'?"))
            return;

        var id=node.id;
        jQuery.post('/index.php',{
                module: "profilemodule",
                action: "manage_invoices_delete",
                ajax_action: 1,
                src: __FormHelper_GetSrc(),
                id: id,
                t:Math.random()
            },function (data){
                _invoices_reload();
        });
    }
}

function _invoices_reload()
{
    jQuery('#_tab_profile_invoices').datagrid("reload");
}

function _invoices_edit(add_new){
    // need to clear old data first
    _invoices_pdf_uploader.clearStoredFiles();
    _invoices_img_uploader.clearStoredFiles();
    jQuery("#_invoices_form_name").val("");
    jQuery("#_invoices_form_filepdf").val("");
    jQuery("#_invoices_form_invoice_id").val(0);
    jQuery("#_invoices_form_fileimg").val("");
    // load edit data if available

    var node=jQuery('#_tab_profile_invoices').datagrid("getSelected");
    SetWindowPanelTitle("_invoices_dialog_edit","Add invoice");
    if (add_new != true && node != null)
        {
        SetWindowPanelTitle("_invoices_dialog_edit","Edit invoice "+node.name);
        jQuery("#_invoices_form_name").val(node.name);
        jQuery("#_invoices_form_filepdf").val(node.pdf_file);
        jQuery("#_invoices_form_fileimg").val(node.img_file);
        jQuery("#_invoices_form_invoice_id").val(node.id);
    }
    else if (add_new != true && node == null)
        // add_new == false ; node == null
        {
        return;
    }
    // then show 
    jQuery('#_invoices_dialog_edit').dialog("open");    
}
/* end invoices functions */


/* Meeting functions */
function _meeting_toolbar_meetingstate()
{
    var state = jQuery("#_meeting_toolbar_meetingstate").attr("checked");
    if (state == "checked")
        {
        //unhide
        jQuery("#_tab_profile_meeting").datagrid("showColumn", "approve_date");
        jQuery("#_tab_profile_meeting").datagrid("showColumn", "approve_by");
        jQuery("#_tab_profile_meeting").datagrid("showColumn", "approve_state");
        jQuery("#_tab_profile_meeting").datagrid("showColumn", "approve_note");
    }
    else
        {
        //unhide
        jQuery("#_tab_profile_meeting").datagrid("hideColumn", "approve_date");
        jQuery("#_tab_profile_meeting").datagrid("hideColumn", "approve_by");
        jQuery("#_tab_profile_meeting").datagrid("hideColumn", "approve_state");
        jQuery("#_tab_profile_meeting").datagrid("hideColumn", "approve_note");
    }
}

function _meeting_toolbar_mentorstate()
{
    var state = jQuery("#_meeting_toolbar_mentorstate").attr("checked");
    if (state == "checked")
        {
        //unhide
        jQuery("#_tab_profile_meeting").datagrid("showColumn", "mentor_request");
        jQuery("#_tab_profile_meeting").datagrid("showColumn", "mentor_required");
        jQuery("#_tab_profile_meeting").datagrid("showColumn", "mentor_language");
    }
    else
        {
        //unhide
        jQuery("#_tab_profile_meeting").datagrid("hideColumn", "mentor_request");
        jQuery("#_tab_profile_meeting").datagrid("hideColumn", "mentor_required");
        jQuery("#_tab_profile_meeting").datagrid("hideColumn", "mentor_language");
    }
}

function _meeting_toolbar_meetingdetailstate()
{
    var state = jQuery("#_meeting_toolbar_meetingdetailstate").attr("checked");
    if (state == "checked")
        {
        //unhide
        jQuery("#_tab_profile_meeting").datagrid("showColumn", "meeting_record");
        jQuery("#_tab_profile_meeting").datagrid("showColumn", "related_lawyers");
        jQuery("#_tab_profile_meeting").datagrid("showColumn", "previous_subject");
        jQuery("#_tab_profile_meeting").datagrid("showColumn", "meeting_notes");
        jQuery("#_tab_profile_meeting").datagrid("showColumn", "last_update");
        jQuery("#_tab_profile_meeting").datagrid("showColumn", "last_update_by");
    }
    else
        {
        //unhide
        jQuery("#_tab_profile_meeting").datagrid("hideColumn", "meeting_record");
        jQuery("#_tab_profile_meeting").datagrid("hideColumn", "related_lawyers");
        jQuery("#_tab_profile_meeting").datagrid("hideColumn", "previous_subject");
        jQuery("#_tab_profile_meeting").datagrid("hideColumn", "meeting_notes");
        jQuery("#_tab_profile_meeting").datagrid("hideColumn", "last_update");
        jQuery("#_tab_profile_meeting").datagrid("hideColumn", "last_update_by");
    }
}

function _meeting_clearfilter()
{
    try{
        jQuery("#_meeting_advancesearch_name").searchbox("setValue","");
        jQuery("#_meeeting_form_advancesearch_datefrom").datebox("setValue","");
        jQuery("#_meeting_form_advancesearch_dateto").datebox("setValue","");
        jQuery("#_meeting_form_advancesearch_status").combobox("setValue","-1");
    }
    catch (err)
    {

    }
}

function _meeting_reload()
{
    jQuery('#_tab_profile_meeting').datagrid("reload");
}

function _meetings_init_dialog()
{
    // for meeting approve
    jQuery("#_meeting_detail_state_saveSubmit").click(function(){
            if (jQuery("#_meeting_detail_approve_note").val()=="")
                {
                alert("Please enter your reason");
                jQuery("#_meeting_detail_approve_note").focus();
                return false;
            }
            else
                {
                var node=jQuery('#_tab_profile_meeting').datagrid("getSelected");
                _abpv_showLoading();  
                jQuery.post("/index.php", {
                        module: 'profilemodule',
                        action: 'manage_meeting_approve',
                        ajax_action: '1',
                        id: node.id,
                        src: __FormHelper_GetSrc(),
                        approve_state: jQuery("#_meeting_detail_approve_state").combobox("getValue"),
                        approve_note:  jQuery("#_meeting_detail_approve_note").val(),
                        t: Math.random()
                    }, function(param)
                    {
                        _abpv_hideLoading();
                    }
                );
            }
    });

    // for meeting reply
    jQuery("#meeting_reply_sendSubmit").click(function(){

            var node=jQuery('#_tab_profile_meeting').datagrid("getSelected");
            _abpv_showLoading();  
            jQuery.post("/index.php", {
                    module: 'profilemodule',
                    action: 'manage_meeting_reply',
                    ajax_action: '1',
                    id: node.id,
                    src: __FormHelper_GetSrc(),
                    subject: jQuery("#meeting_reply_subject").val(),
                    content:  jQuery("textarea[name='meeting_reply_content']").val(),
                    t: Math.random()
                }, function(param)
                {
                    _abpv_hideLoading();
                    var  data = ajax_string_dim(param);
                    if (data == "OK")
                        {
                        alert("Mail send successfully");
                    }
                    else
                        {            
                        alert("Mail send failed");
                    }
                }
            );
            return false;
    });

    _meeting_history();
    _meeting_note();
}
function _meeting_history()
{
    jQuery('#_meeting_detail_history').datagrid({  

            autoRowHeight: true, 
            idField: "id",
            nowrap: false,
            height: 415,
            pageNumber: 1,
            pageSize: 20,
            pagination: 1,
            rownumbers: false,
            singleSelect: true, 
            showHeader:true,
            columns:[[  
                    {field:'date',title:'Date',width:130, sortable:true}, 
                    {field:'action',title:'Action',width:100, sortable:true},                       
                    {field:'content',title:'Description',width:100, sortable:true},                       
                    {field:'action_by',title:'Actor',width:100, sortable:true},                       
                ]],
            onBeforeLoad: function(param){
                if(!_grid_meeting_load)
                return false;
                // get selected user in main grid
                var node=jQuery('#_tab_profile_meeting').datagrid("getSelected");
                if (node == null)
                    return;
                var url="/index.php?module=profilemodule&action=manage_meeting_history_ajax&ajax_action=1&id="+node.id+"&src="+__FormHelper_GetSrc();
                // build URL to reload
                var options=jQuery(this).datagrid("options");

                // date search type           
                url+="&_"+Math.random();            
                jQuery(this).datagrid('options').url = url;
            },
            onClickRow:    function(param){
                history_click(param);           
            }

    });

}

function _meeting_note()
{
    jQuery('#_meeting_detail_note').datagrid({  

            autoRowHeight: true, 
            idField: "id",
            nowrap: false,
            height: 415,
            pageNumber: 1,
            pageSize: 20,
            pagination: 1,
            rownumbers: false,
            singleSelect: true, 
            showHeader:true,
            columns:[[  
                    {field:'post_date',title:'Date',width:130, sortable:true, formatter: function(value,row,index){
                            return row.post_date_text;
                    }}, 
                    {field:'subject',title:'Subject',width:100, sortable:true},                       
                    {field:'post_by',title:'Post by',width:100},                       
                ]],
            onBeforeLoad: function(param){
                if(!_grid_meeting_load)
                return false;
                // get selected user in main grid
                var node=jQuery('#_tab_profile_meeting').datagrid("getSelected");
                if (node == null)
                    return;
                var url="/index.php?module=profilemodule&action=manage_meeting_notes_ajax&ajax_action=1&id="+node.id+"&src="+__FormHelper_GetSrc();
                // build URL to reload
                var options=jQuery(this).datagrid("options");

                var search_name=jQuery("#_meeting_detail_search_note").searchbox("getValue");

                if(search_name!=''){
                    url += "&search="+search_name;
                }            
                // date search type           
                url+="&_"+Math.random();            
                jQuery(this).datagrid('options').url = url;
            },
            onClickRow:    function(param){
                meeting_note_click();           
            }

    });

}

function _meeting_note_search()
{
    jQuery('#_meeting_detail_note').datagrid("reload");
}

function _meeting_history_search ()
{
    jQuery('#_meeting_detail_history').datagrid("reload");
}

function history_click(data)
{
    node=jQuery('#_meeting_detail_history').datagrid("getSelected");
    html="<p><b>Meeting id:</b> "+node.meeting_id+"</p>"+
    "<p><b>Action:</b> "+node.action+"</p>"+
    "<p><b>Do action by:</b> "+node.action_by+"</p>"+
    "<p><b>Content: </b>"+node.content+"</p>"+
    "<p><b>Time:</b> "+node.date+"</p>"
    ;

    jQuery("#_meeting_detail_history_detail").html(html);

}

function meeting_note_click()
{
    node=jQuery('#_meeting_detail_note').datagrid("getSelected");
    if (node == null)
        return;
    html="<p><b>Meeting id:</b> "+node.meeting_id+"</p>"+
    "<p><b>Time:</b> "+node.post_date_text+"</p>"+
    "<p><b>Subject:</b> "+node.subject+"</p>"+
    "<p><b>Content: </b>"+node.content+"</p>";     
    jQuery("#_meeting_detail_note_detail").html(html);

}
function _new_meeting_note() 
{
    jQuery("#_meeting_detail_note_edit").val(0);
    SetWindowPanelTitle("_meeting_notes_dialog_edit","Add note");
    jQuery("#_meeting_detail_new_note_subject").val("");
    jQuery("#_meeting_detail_new_note_content").val("");
    jQuery('#_meeting_notes_dialog_edit').dialog("open");
}

function _edit_meeting_note()
{
    node=jQuery('#_meeting_detail_note').datagrid("getSelected");  
    if (node == null)  
        return ;
    SetWindowPanelTitle("_meeting_notes_dialog_edit","Edit note");
    jQuery('#_meeting_notes_dialog_edit').dialog("open");
    jQuery("#_meeting_detail_new_note_subject").val(node.subject);
    jQuery("#_meeting_detail_new_note_content").val(node.content);
    jQuery("#_meeting_detail_note_edit").val(node.id);

}
function _del_meeting_note()
{
    var node=jQuery('#_meeting_detail_note').datagrid("getSelected");   
    if (node != null)
        {
        if (!confirm("Do you want to delete note '"+node.subject+"'?"))
            return;

        var id=node.id;
        jQuery.post('/index.php',{
                module: "profilemodule",
                action: "manage_meeting_notes_delete",
                ajax_action: 1,
                src: __FormHelper_GetSrc(),
                id: id,
                t:Math.random()
            },function (data){
                jQuery('#_meeting_detail_note').datagrid("reload");
        });
    }
}
function _new_meeting_note_post() 
{
    _abpv_showLoading();

    node=jQuery('#_tab_profile_meeting').datagrid("getSelected");
    jQuery.post("/index.php", {
            module: 'profilemodule',
            action: 'manage_meeting_notes_ajax',
            ajax_action: '1',
            subject:jQuery("#_meeting_detail_new_note_subject").val(),
            _new:1,
            edit:jQuery("#_meeting_detail_note_edit").val(),
            id:node.id,
            src: __FormHelper_GetSrc(),
            content: jQuery("#_meeting_detail_new_note_content").val(),
            t: Math.random()
        }, function(param)
        {
            _abpv_hideLoading();
            jQuery("#_meeting_note_")[0].reset();
            jQuery('#_meeting_detail_note').datagrid("reload");
            jQuery('#_meeting_notes_dialog_edit').dialog("close");

        }
    );

}
function _meeting_edit(){
    var node=jQuery('#_tab_profile_meeting').datagrid("getSelected");
    if (node != null)
        {
        _grid_meeting_load=true;
        // refresh note and history
        jQuery("#_meeting_detail_search_history").searchbox("setValue","");
        jQuery("#_meeting_detail_search_note").searchbox("setValue","");
        _meeting_note_search();
        _meeting_history_search();

        SetWindowPanelTitle("_meeting_dialog_edit", "Meeting detail. ID = "+node.id+" ; Requested by "+node.user_target+" on "+node.request_date_text);

        // tab actions
        jQuery("#_meeting_detail_approve_state").combobox("setValue", node.approve_state);
        jQuery("#_meeting_detail_approve_note").val(node.approve_note);
        if (node.approve_state > 0)
            {
            jQuery("#_meeting_detail_approve_detail").html("Reviewed on "+node.approve_date_text+" by "+node.approve_by);
        }
        else
            {
            jQuery("#_meeting_detail_approve_detail").html("");
        }

        // tab reply
        jQuery("#meeting_reply_source_email").val(node.email);
        jQuery("#meeting_reply_sender").val(_user_name + " <"+_user_email+">");
        jQuery("#meeting_reply_subject").val("Reply meeting request on "+node.request_date_text);
        var html="";
        html+="Dear "+node.user_target+" ("+node.email+")\r\n\r\n\r\n";
        html+="Enter your feedback here\r\n\r\n";
        html+="On "+node.request_date_text+", Kurtulus Law Office received your meeting request. Here is meeting request summary:\r\n";
        html+="Meeting date: "+node.meeting_date_text+"\r\n";
        html+="Meeting duration: "+node.meeting_duration+"mins\r\n";
        html+="Meeting place: "+node.meeting_place+"\r\n";
        html+="Meeting language: "+node.meeting_language+"\r\n";
        html+="Meeting type: "+node.meeting_type+"\r\n";
        html+="Meeting record: "+((node.meeting_record==0)?"No":"Yes")+"\r\n";
        
        html+="Translator Request: "+((node.translator_request==0)?"No":"Yes")+"\r\n";
        
        html+="Related Lawyers: "+((typeof node.related_lawyers === 'undefined')?"No":(node.related_lawyers))+"\r\n";
        html+="Mentor Request: "+((node.mentor_request==0)?"No":"Yes")+"\r\n";       
        html+="Mentor required: "+node.mentor_required+"\r\n";
       
        html+="Meeting note: "+node.meeting_notes+"\r\n";
        
        html+="\r\n\r\nThanks & Best Regards\r\n"+_user_name;
        jQuery("#meeting_reply_content").val(html);

        // url of meeting detail
        var src=__FormHelper_GetSrc();
        var url="/index.php?module=profilemodule&action=manage_meeting_detail&ajax_action=1&src="+src+"&id="+node.id;
        _abpv_showLoading();       
        // for tab profile detail
        jQuery.get(url, function(data) {
                jQuery("#meeting_detail_warpper").html(data);
                jQuery('#_meeting_dialog_edit').dialog("open");
                _abpv_hideLoading();
        });
    }
}


/* for form helper */
var form_syslog_preinit = function(){
}

var form_onBeforeLoad = function(param)
{   
    jQuery(this).datagrid('options').url =__Manager_Link_BuildUrl() ;
};

/**
* Handle grid double click event
*/
var grid_syslog_dbrow = function(index,row){
    _profile_edit();
}

function __Manager_Link_BuildUrl(){
    var url=__FormHelper_GetActionLink(); 
    var options=__FormHelper_GetGridObject().datagrid("options");

    var search_value = jQuery("#_profile_as_search").searchbox("getValue");
    var search_type = jQuery("#_profile_as_search").searchbox("getName");

    if (search_type == "new_meetings")
        {
        url+="&meeting_new_only=1";
        search_type="meeting";
    }

    if(search_value == 'Please enter your search')
        {
        search_value="";
    }
    url += "&search="+search_value+"&search_type="+search_type;

    // date search type
    var date_from =jQuery("#_profile_as_datefrom").datebox("getValue");
    var date_to=jQuery("#_profile_as_dateto").datebox("getValue");

    url+="&datefrom="+date_from+"&dateto="+date_to;

    if (options.sortName != null){
        url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
    }

    if (options.sortName != null)
        {
        url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
    }
    url+="&t="+Math.random();

    return url;

}

function _profile_clearfilter(){
    try{
        jQuery("#_profile_as_search").searchbox("setValue","");
        jQuery("#_profile_as_datefrom").datebox("setValue","");
        jQuery("#_profile_as_dateto").datebox("setValue","");
    }
    catch (err)
    {

    }
}

function __AB_PDoremi_CityManager_Remove(){

    var node = __FormHelper_GetGridObject().datagrid('getSelected');
    var temp=Math.random()*9999;
    if (node){
        if (!confirm("Are you sure deleting this client?"))
            return;
        // call ajax
        jQuery.post("/index.php", {
                module: 'profilemodule',
                action: 'delete',
                ajax_action: '1',
                id: node.id,
                src: __FormHelper_GetSrc(),
                t: temp
            }, function(param)
            {
                __FormHelper_GetGridObject().datagrid('reload');
            }
        );
        __FormHelper_GetGridObject().datagrid('clearSelections');
    }
}

function SetWindowPanelTitle(dialog_id, title){
    jQuery('#'+dialog_id).dialog("window").find(".panel-title:first").html(title);
}

function SetGridPanelTitle(dialog_id, title){
    jQuery("#"+dialog_id).find(".panel-title:first").html(title);
}
